<?php
namespace Qy {
    /**
    * 企业微信标签管理类
    * @author Jamers
    * @since 2017.1.1
    * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%A0%87%E7%AD%BE
    */
    class Tag {
        private $common;
        public function __construct($ary = array()) {
            if (isset($ary['common'])) {
                $this->common = &$ary['common'];
            }else{
                $this->common = new \Qy\Common();
            }
        }
        /**
        * 创建标签
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%A0%87%E7%AD%BE
        * 
        * @param mixed $ary
        * @param mixed $a_token
        */
        public function createTag($ary = array(), $a_token = '') {
            if (empty($ary)) return false;
            if (!isset($ary['tagname'])) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/tag/create?access_token={$a_token}";
            return $this->common->httpRequest($url,$ary,'post');
        }
        /**
        * 更新标签名字
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%A0%87%E7%AD%BE
        * 
        * @param mixed $ary
        * @param mixed $a_token
        */
        public function updateTag($ary = array(), $a_token = '') {
            if (empty($ary)) return false;
            if (!isset($ary['tagname']) || !isset($ary['tagid'])) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/tag/update?access_token={$a_token}";
            return $this->common->httpRequest($url,$ary,'post');
        }
        
        /**
        * 删除标签
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%A0%87%E7%AD%BE
        * 
        * @param mixed $tagid
        * @param mixed $a_token
        */
        public function deleteTag($tagid,$a_token = '') {
            if (empty($tagid)) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/tag/delete?access_token={$a_token}&tagid={$tagid}";
            return $this->common->httpRequest($url);
        }
        /**
        * 获取标签成员
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%A0%87%E7%AD%BE
        * 
        * @param mixed $tagid
        * @param mixed $a_token
        */
        public function getTagUsers($tagid,$a_token = '') {
            if (empty($tagid)) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/tag/get?access_token={$a_token}&tagid={$tagid}";
            return $this->common->httpRequest($url);
        }
        
        /**
        * 增加标签成员
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%A0%87%E7%AD%BE
        * 
        * @param mixed $ary
        * @param mixed $a_token
        */
        public function addTagUsers($ary = array(),$a_token = '') {
            if (empty($ary)) return false;
            if (!isset($ary['tagid'])) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/tag/addtagusers?access_token={$a_token}";
            return $this->common->httpRequest($url,$ary,'post');
        }
        
        /**
        * 删除标签成员
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%A0%87%E7%AD%BE
        * 
        * @param mixed $ary
        * @param mixed $a_token
        */
        public function delTagUsers($ary = array(),$a_token = '') {
            if (empty($ary)) return false;
            if (!isset($ary['tagid'])) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/tag/deltagusers?access_token={$a_token}";
            return $this->common->httpRequest($url,$ary,'post');
        }
        
        /**
        * 获取标签列表
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%A0%87%E7%AD%BE
        * 
        * @param mixed $a_token
        */
        public function getTagList($a_token = '') {
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/tag/list?access_token={$a_token}";
            return $this->common->httpRequest($url);
        }
    }
}
?>
